package com.meritdata.cloud.data.standard.naming.controller;

import com.meritdata.cloud.data.standard.naming.entity.TdDataStandardNaming;
import com.meritdata.cloud.data.standard.naming.service.ITdDataStandardNamingService;
import com.meritdata.cloud.resultmodel.ResultBody;
import com.meritdata.cloud.resultmodel.ResultStatus;
import com.meritdata.cloud.utils.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Pageable;
import java.util.Date;

//命名规范

@RestController
@RequestMapping("/api/standard/naming")
@Api(value = "命名规范-NamingController",tags = "命名规范")
public class TdDataStandardNamingController {

	@Autowired
	private SessionUtils sessionUtils;

	@Autowired
	private ITdDataStandardNamingService tdDataStandardNamingService;

	@RequestMapping(value = "/add",method = RequestMethod.POST)
	@ApiOperation(value="新增命名规范", notes = "保存命名规范信息")
	public ResultBody<TdDataStandardNaming> add(@ApiParam(value = "规则名称", required = true) @RequestParam String name,
												@ApiParam(value = "适用对象", required = true) @RequestParam String applyObject,
												@ApiParam(value = "规则内容", required = false) @RequestParam String ruleContent,
												@ApiParam(value = "名称最大长度", required = false) @RequestParam Integer maxNameLength,
												@ApiParam(value = "备注说明", required = false) @RequestParam String remark){
		TdDataStandardNaming tdDataStandardNaming = new TdDataStandardNaming(name,applyObject,ruleContent,maxNameLength,remark,2,new Date(),new Date(),Long.valueOf(sessionUtils.getCurrentUserId()));
		try {
			return ResultBody.success(tdDataStandardNamingService.addNamingData(tdDataStandardNaming));
		} catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}
	@RequestMapping(value = "/list",method = RequestMethod.POST)
	@ApiOperation(value = "命名规范查询接口", notes = "获取命名规范信息")
	public ResultBody<TdDataStandardNaming> qualityRuleList(@ApiParam(value = "页数", required = true) @RequestParam Integer page,
															@ApiParam(value = "条数", required = true) @RequestParam Integer rows,
															@ApiParam(value = "搜索名称", required = false) @RequestParam String name) {
		try {
			return ResultBody.success(tdDataStandardNamingService.selectNamingAll(page,rows,name));
		} catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

	@RequestMapping(value = "/del",method = RequestMethod.POST)
	@ApiOperation(value = "删除命名规范接口", notes = "删除命名规范信息")
	public ResultBody del(@ApiParam(value = "删除规范的ids", required = true) @RequestParam Long id) {
		try {
			tdDataStandardNamingService.deleteNamingById(id);
			return ResultBody.success(true);
		} catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@ApiOperation(value = "修改命名规范接口", notes = "修改命名规范信息")
	public ResultBody<TdDataStandardNaming> update(@ApiParam(value = "修改规范的id", required = true) @RequestParam Long id,
												   @ApiParam(value = "规范名称", required = true) @RequestParam String name,
												   @ApiParam(value = "适用对象", required = true) @RequestParam String applyObject,
												   @ApiParam(value = "规则内容", required = false) @RequestParam String ruleContent,
												   @ApiParam(value = "名字最大长度", required = false)@RequestParam Integer maxNameLength,
												   @ApiParam(value = "备注说明", required = false)@RequestParam String remark) {
		try {
			tdDataStandardNamingService.updateNamingDataById(id,name,applyObject,ruleContent,maxNameLength,remark);
			return ResultBody.success(true);
		}catch (RuntimeException e){
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

	@RequestMapping(value = "/update/state",method = RequestMethod.POST)
	@ApiOperation(value = "修改命名规范状态接口", notes = "修改命名规范状态信息")
	public ResultBody updateState(@ApiParam(value = "修改规范的ids", required = true) @RequestParam Long[] ids,
								  @ApiParam(value = "修改规范的状态", required = true) @RequestParam Integer status) {
		ResultBody resultBody = new ResultBody();
		for (Long id:ids) {
			try {
				tdDataStandardNamingService.updateNamingStatus(id,status);
				resultBody.success(true);
			}catch (RuntimeException e){
				resultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
			}
		}
		return resultBody;
	}
	@RequestMapping(value = "/exist",method = RequestMethod.POST)
	@ApiOperation(value = "规则名称是否存在接口", notes = "规则名称是否存在")
	public ResultBody isExistName(@ApiParam(value = "规则名称规范的名称", required = true) @RequestParam String name) {

		try {
			return ResultBody.success(tdDataStandardNamingService.existName(name));
		} catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}
}
